package com.lyandwc.lw_bbs.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lyandwc.lw_bbs.dao.CommentDao;
import com.lyandwc.lw_bbs.entity.Comment;
import com.lyandwc.lw_bbs.service.CommentService;
import com.lyandwc.lw_bbs.service.TopicService;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.List;

@Service
public class CommentServiceImpl extends ServiceImpl<CommentDao, Comment> implements CommentService {
    private TopicService topicService;

    public CommentServiceImpl(TopicService topicService) {
        this.topicService = topicService;
    }

    @Override
    public Comment findAbbrCommentById(int id) {
        return this.baseMapper.findAbbrCommentById(id);
    }

    @Override
    public Comment findCommentById(int id) {
        return this.baseMapper.findCommentById(id);
    }

    @Override
    public List<Comment> listCommentsByTopicId(int topicId, int page, int limit) {
        return this.baseMapper.listCommentsByTopicId(topicId, (page - 1) * limit, limit);
    }

    @Override
    public Comment insertReplyComment(String content, int replyCommentId, int uid) {
        Comment comment = new Comment();
        Comment replyComment = this.findAbbrCommentById(replyCommentId);

        comment.setContent(content);
        comment.setCreateAt(new Timestamp(System.currentTimeMillis()));
        comment.setReplyCommentId(replyCommentId);
        comment.setUserId(uid);
        comment.setTopicId(replyComment.getTopicId());

        this.save(comment);

        return comment;
    }

    @Override
    public Comment updateComment(int id, String content) {
        Comment comment = this.getById(id);
        comment.setContent(content);
        this.updateById(comment);
        return comment;
    }

    @Override
    public Comment likeComment(int id) {
        Comment comment = this.getById(id);
        comment.setLikeCount(comment.getLikeCount() + 1);
        this.updateById(comment);
        return comment;
    }

    @Override
    public Comment dislikeComment(int id) {
        Comment comment = this.getById(id);
        comment.setDislikeCount(comment.getDislikeCount() + 1);
        this.updateById(comment);
        return comment;
    }
}
